<template>
  <!-- 营业场所信息 -->
  <view class="bpremisesInfo">
    <!-- <view class="wrapper" :class="dataFlag == '' ? 'wapHeight' : ''"> 不要删 -->
    <view class="wrapper">
      <view class="wrapper-item">
        <view v-if="dataFlag == ''">
          <u-empty
            mode="list"
            text="您还没有完善场所信息！"
            :icon="baseImgUrl + 'no-icon4.png'"
          >
          </u-empty>

          <!--不要删 <view class="addbtns">
            <button
              class="submit-btn"
              type="primary"
              @click="dataFlag = 1"
              icon="map"
              style="width: 50%; border-radius: 8rpx"
            >
              添加营业场所信息
            </button>
          </view> -->
        </view>

        <view v-if="dataFlag != ''">
          <u-cell
            :titleStyle="titleStyle"
            title="开业时间"
            clickable
            isLink
            :rightIconStyle="rightIconStyle"
            @click="cellTimeClick('openingTimeDesc')"
          >
            <view
              slot="value"
              :class="form.openingTimeDesc ? 'cell-noEmpty' : 'cell-empty'"
            >
              {{ form.openingTimeDesc || "请选择" }}
            </view>
          </u-cell>

          <u-cell-group :border="false">
            <u-cell :titleStyle="titleStyle" title="营业面积（㎡）">
              <u-input
                v-model="form.businessArea"
                data-type="workPerson"
                placeholder="请输入"
                slot="value"
                fontSize="14"
                placeholderClass="placeholderClass"
                inputAlign="right"
                border="none"
                :maxlength="8"
                :required="true"
                :readonly="dataFlag == 3 ? true : false"
                @blur="areaHandle"
              ></u-input>
            </u-cell>
          </u-cell-group>

          <u-cell
            :titleStyle="titleStyle"
            title="房屋结构"
            clickable
            isLink
            :rightIconStyle="rightIconStyle"
            @click="cellClick('text', 'houseStructure')"
          >
            <view
              slot="value"
              :class="form.houseStructureDesc ? 'cell-noEmpty' : 'cell-empty'"
            >
              {{ form.houseStructureDesc || "请选择" }}
            </view>
          </u-cell>

          <u-cell
            :titleStyle="titleStyle"
            title="房屋性质"
            clickable
            isLink
            :rightIconStyle="rightIconStyle"
            @click="cellClick('text', 'houseNature')"
          >
            <view
              slot="value"
              :class="form.houseNatureDesc ? 'cell-noEmpty' : 'cell-empty'"
            >
              {{ form.houseNatureDesc || "请选择" }}
            </view>
          </u-cell>

          <u-cell
            :titleStyle="titleStyle"
            title="房屋所有权"
            clickable
            isLink
            :rightIconStyle="rightIconStyle"
            @click="cellClick('text', 'houseOwnership')"
          >
            <view
              slot="value"
              :class="form.houseOwnershipDesc ? 'cell-noEmpty' : 'cell-empty'"
            >
              {{ form.houseOwnershipDesc || "请选择" }}
            </view>
          </u-cell>

          <u-cell
            :titleStyle="titleStyle"
            title="营业建筑是否在1998年9月1日之前竣工建筑且此后未改建(含装修、用途变更)"
            clickable
            isLink
            :rightIconStyle="rightIconStyle"
            @click="cellClick('text', 'onRebuild')"
          >
            <view
              slot="value"
              :class="form.onRebuildDesc ? 'cell-noEmpty' : 'cell-empty'"
              style="min-width: 84rpx; text-align: right"
            >
              {{ form.onRebuildDesc || "请选择" }}
            </view>
          </u-cell>

          <u-cell
            :titleStyle="titleStyle"
            title="是否依法通过消防验收"
            clickable
            isLink
            :rightIconStyle="rightIconStyle"
            @click="cellClick('text', 'onFireAcceptance')"
          >
            <view
              slot="value"
              :class="form.onFireAcceptanceDesc ? 'cell-noEmpty' : 'cell-empty'"
            >
              {{ form.onFireAcceptanceDesc || "请选择" }}
            </view>
          </u-cell>

          <u-cell
            :titleStyle="titleStyle"
            title="是否依法进行竣工验收消防备案"
            clickable
            isLink
            :rightIconStyle="rightIconStyle"
            @click="cellClick('text', 'onFireFiling')"
          >
            <view
              slot="value"
              :class="form.onFireFilingDesc ? 'cell-noEmpty' : 'cell-empty'"
            >
              {{ form.onFireFilingDesc || "请选择" }}
            </view>
          </u-cell>

          <u-cell
            :titleStyle="titleStyle"
            title="是否为公众聚集场所"
            clickable
            isLink
            :rightIconStyle="rightIconStyle"
            @click="cellClick('text', 'onGatheringPlace')"
          >
            <view
              slot="value"
              :class="form.onGatheringPlaceDesc ? 'cell-noEmpty' : 'cell-empty'"
            >
              {{ form.onGatheringPlaceDesc || "请选择" }}
            </view>
          </u-cell>

          <u-cell
            :titleStyle="titleStyle"
            title="依法通过投入使用、营业前消防安全检查"
            clickable
            isLink
            :rightIconStyle="rightIconStyle"
            @click="cellClick('text', 'onSecurityCheck')"
          >
            <view
              slot="value"
              :class="form.onSecurityCheckDesc ? 'cell-noEmpty' : 'cell-empty'"
            >
              {{ form.onSecurityCheckDesc || "请选择" }}
            </view>
          </u-cell>
        </view>
      </view>
    </view>

    <view class="btns" v-if="dataFlag == ''">
      <button
        class="submit-btn"
        type="primary"
        @click="dataFlag = 1"
        icon="map"
        style="width: 100%"
      >
        添加营业场所信息
      </button>
    </view>
    <view class="btns" v-if="dataFlag != ''">
      <block v-if="dataFlag == 1 || dataFlag == 2">
        <button
          class="reset-btn lt cancelBtn"
          type="primary"
          @click="cancelHandle"
        >
          取消
        </button>
        <button class="submit-btn" type="primary" @click="sunmitHandle">
          保存
        </button>
      </block>
      <button
        v-if="dataFlag == 3"
        class="lookbtn"
        type="primary"
        @click="dataFlag = 2"
        style="width: 100%"
      >
        修改
      </button>
    </view>

    <u-picker
      v-if="pickerShow"
      :show="pickerShow"
      :columns="columns"
      :keyName="keyName"
      @confirm="showConfirm"
      @close="pickerClose"
      @cancel="pickerCancel"
      :closeOnClickOverlay="true"
    ></u-picker>

    <!-- 时间选择器 -->
    <u-datetime-picker
      ref="datetimePicker"
      :show="datetimeShow"
      v-model="timeValue"
      mode="date"
      :formatter="formatter"
      :minDate="minDate"
      :maxDate="maxDate"
      @close="pickerTimeClose"
      @cancel="pickerTimeClose(1)"
      @confirm="showTimeConfirm"
      :closeOnClickOverlay="true"
    ></u-datetime-picker>
  </view>
</template>

<script>
import { findCompany, infoDetailSave } from "@/api/home.js";
import { findEnableDictItemsbyCode } from "@/api/api.js";
import {
  handleClickRouter,
  formatters,
  hiddenInfo,
  pvwImage,
  Toast,
  navBack,
  uniTrim,
  isNumberTwo,
  dayTurnTime,
  ifUndefined,
} from "@/utils/util.js";
import mixin from "@/utils/mixin.js";

export default {
  name: "bpremisesInfo",
  mixins: [mixin],
  data() {
    return {
      baseImgUrl: this.$baseImgUrl,
      form: {},
      options: {},
      errorType: ["none"],
      dataFlag: "", // 1:添加  2编辑  3修改
    };
  },
  onLoad(options) {
    this.options = options;
    this.getList();
  },
  methods: {
    handleClickRouter,
    hiddenInfo,
    formatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      }
      if (type === "month") {
        return `${value}月`;
      }
      if (type === "day") {
        return `${value}日`;
      }
      return value;
    },
    cellTimeClick(e) {
      if (this.dataFlag == 3) return false;
      this.cellType = e;
      var minDateNum = +new Date(); //获取当前时间的毫秒数

      switch (this.cellType) {
        case "openingTimeDesc":
          this.minDate = new Date(1971, 0, 1).getTime();
          this.maxDate = minDateNum;
          this.timeValue = this.maxDate;
          break;

        default:
          break;
      }

      setTimeout(() => {
        this.datetimeShow = true;
      }, 100);
    },
    pickerTimeClose(e) {
      if (e) {
        switch (this.cellType) {
          case "openingTimeDesc":
            this.form.openingTimeDesc = "";
            this.form.openingTime = "";
            break;
          default:
            break;
        }
      }
      this.datetimeShow = false;
    },
    /* 时间选择器 确定 */
    showTimeConfirm(e) {
      let arr = e.value;
      if (arr) {
        switch (this.cellType) {
          case "openingTimeDesc":
            this.form.openingTimeDesc = formatters(arr, "YY/MM/DD");
            this.form.openingTime = arr;
            break;
          default:
            break;
        }
      }
      this.datetimeShow = false;
    },
    /* cell 点击 */
    cellClick(name, type) {
      if (this.dataFlag == 3) return false;

      this.keyName = name;
      this.cellType = type;
      let code = "";
      switch (this.cellType) {
        case "houseStructure":
          code = "house_structure_type";
          break;
        case "houseNature":
          code = "house_nature";
          break;
        case "houseOwnership":
          code = "house_ownership_type";
          break;
        case "onRebuild":
          code = "flag_type";
          break;
        case "onFireAcceptance":
          code = "flag_type";
          break;
        case "onFireFiling":
          code = "flag_type";
          break;
        case "onGatheringPlace":
          code = "flag_type";
          break;
        case "onSecurityCheck":
          code = "flag_type";
          break;
        default:
          break;
      }

      findEnableDictItemsbyCode(code).then((res) => {
        this.columns[0] = res.result || [];
        this.pickerShow = true;
      });
    },
    /* 选择器 确定 */
    showConfirm(e) {
      let arr = e.value;
      if (arr.length) {
        let obj = arr[0];
        switch (this.cellType) {
          case "houseStructure":
            this.form.houseStructure = obj.value;
            this.form.houseStructureDesc = obj.title;
            break;
          case "houseNature":
            this.form.houseNature = obj.value;
            this.form.houseNatureDesc = obj.title;
            break;
          case "houseOwnership":
            this.form.houseOwnership = obj.value;
            this.form.houseOwnershipDesc = obj.title;
            break;
          case "onRebuild":
            this.form.onRebuild = obj.value;
            this.form.onRebuildDesc = obj.title;
            break;
          case "onFireAcceptance":
            this.form.onFireAcceptance = obj.value;
            this.form.onFireAcceptanceDesc = obj.title;
            break;
          case "onFireFiling":
            this.form.onFireFiling = obj.value;
            this.form.onFireFilingDesc = obj.title;
            break;
          case "onGatheringPlace":
            this.form.onGatheringPlace = obj.value;
            this.form.onGatheringPlaceDesc = obj.title;
            break;
          case "onSecurityCheck":
            this.form.onSecurityCheck = obj.value;
            this.form.onSecurityCheckDesc = obj.title;
            break;
          default:
            break;
        }
      }
      this.pickerShow = false;
    },
    areaHandle() {
      if (this.form.businessArea && !isNumberTwo(this.form.businessArea)) {
        Toast("营业面积请输入数字,可精确到小数点后两位");
        return;
      }
    },
    cancelHandle() {
      if (this.dataFlag == 1) {
        this.dataFlag = "";
        this.form = {};
      } else if (this.dataFlag == 2) {
        this.dataFlag = 3;
      }
    },
    sunmitHandle() {
      let {
        openingTime,
        businessArea,
        houseStructure,
        houseNature,
        houseOwnership,
        onRebuild,
        onFireAcceptance,
        onFireFiling,
        onGatheringPlace,
        onSecurityCheck,
      } = this.form;

      if (!openingTime) {
        Toast("请选择开业时间");
        return;
      }

      if (!businessArea || !uniTrim(businessArea)) {
        Toast("请输入营业面积");
        return;
      }

      if (businessArea && !isNumberTwo(businessArea)) {
        Toast("营业面积请输入数字,可精确到小数点后两位");
        return;
      }

      if (!houseStructure) {
        Toast("请选择房屋结构");
        return;
      }

      if (!houseNature) {
        Toast("请选择房屋性质");
        return;
      }

      if (!houseOwnership) {
        Toast("请选择房屋所有权");
        return;
      }

      if (ifUndefined(onRebuild)) {
        Toast("请选择是否在1998年9月1日之前竣工");
        return;
      }

      if (ifUndefined(onFireAcceptance)) {
        Toast("请选择是否依法通过消防验收");
        return;
      }

      if (ifUndefined(onFireFiling)) {
        Toast("请选择是否依法进行竣工验收消防备案");
        return;
      }

      if (ifUndefined(onGatheringPlace)) {
        Toast("请选择是否为公众聚集场所");
        return;
      }

      if (ifUndefined(onSecurityCheck)) {
        Toast("请选择依法通过投入使用、营业前消防安全检查");
        return;
      }

      let data = {
        companyId: this.options.companyMessageId,
        openingTime: openingTime,
        businessArea: businessArea,
        houseStructure: houseStructure,
        houseNature: houseNature,
        houseOwnership: houseOwnership,
        onRebuild: onRebuild,
        onFireAcceptance: onFireAcceptance,
        onFireFiling: onFireFiling,
        onGatheringPlace: onGatheringPlace,
        onSecurityCheck: onSecurityCheck,
      };

      infoDetailSave(data).then((res) => {
        if (res.code == 200) {
          Toast("保存成功");
          navBack();
        }
      });
    },
    getList() {
      findCompany({
        companyId: this.options.companyMessageId,
      }).then((res) => {
        if (res.code == 200) {
          if (Object.keys(res.result).length == 0) {
            uni.setNavigationBarTitle({
              title: "完善营业场所信息",
            });
            this.dataFlag = "";
            return false;
          }

          this.dataFlag = 3;
          this.form = res.result;
          this.form.openingTime = dayTurnTime(res.result.openingTime);
          this.form.openingTimeDesc = formatters(
            dayTurnTime(res.result.openingTime),
            "YY/MM/DD"
          );

          let {
            house_structure_type,
            house_nature_type,
            house_ownership_type,
            flag_type,
          } = uni.getStorageSync("allCode");
          let {
            houseStructure,
            houseNature,
            houseOwnership,
            onRebuild,
            onFireAcceptance,
            onFireFiling,
            onGatheringPlace,
            onSecurityCheck,
          } = res.result;

          house_structure_type.map((v) => {
            if (v.value == houseStructure) {
              this.form.houseStructureDesc = v.text;
            }
          });

          house_nature_type.map((v) => {
            if (v.value == houseNature) {
              this.form.houseNatureDesc = v.text;
            }
          });

          house_ownership_type.map((v) => {
            if (v.value == houseOwnership) {
              this.form.houseOwnershipDesc = v.text;
            }
          });

          flag_type.map((v) => {
            if (v.value == onRebuild) {
              this.form.onRebuildDesc = v.text;
            }
          });

          flag_type.map((v) => {
            if (v.value == onFireAcceptance) {
              this.form.onFireAcceptanceDesc = v.text;
            }
          });

          flag_type.map((v) => {
            if (v.value == onFireFiling) {
              this.form.onFireFilingDesc = v.text;
            }
          });

          flag_type.map((v) => {
            if (v.value == onGatheringPlace) {
              this.form.onGatheringPlaceDesc = v.text;
            }
          });

          flag_type.map((v) => {
            if (v.value == onSecurityCheck) {
              this.form.onSecurityCheckDesc = v.text;
            }
          });
        }
      });
    },
  },
};
</script>

<style>
page {
  background-color: #f3f3f3 !important;
  height: 100%;
}
</style>
<style lang="scss" scoped>
.bpremisesInfo {
  flex: 1;
  background: #f9f9f9;

  .wrapper {
    height: calc(100vh - 92px);
    margin: 20rpx;
    background: #fff;
    overflow-y: auto;

    .wrapper-item {
      padding: 20rpx;
      margin-bottom: 20rpx;

      .addbtns {
        margin-top: 40rpx;
      }
    }
  }

  .wapHeight {
    height: calc(100vh - 22px);
  }

  .btns {
    button {
      border-radius: 44rpx;
    }
  }
}
</style>
